package in.miraclesirius.arunachaltimes;

import java.io.IOException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.parser.Tag;
import org.jsoup.select.Elements;

import android.app.Activity;
import android.app.Fragment;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.util.Xml.Encoding;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;

public class MainFragment extends Fragment {

	View rootView;
	String url = "http://www.arunachaltimes.in/";
	String storyDivId = "";
	ProgressDialog mProgressDialog;
	String pageTitle = "";

	public MainFragment() {
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		rootView = (View) inflater.inflate(R.layout.fragment_full_story,
				container, false);

		Bundle pigeon = this.getArguments();
		pageTitle = pigeon.getString(MainActivity.PAGE_TITLE);
		url += pigeon.getString(MainActivity.RELATIVE_URL);
		storyDivId = pigeon.getString(MainActivity.DIV_ID);

		new MainStory(getActivity()).execute();

		return rootView;
	}

	private class MainStory extends AsyncTask<Void, Void, Void> {
		String Story;
		Activity context;

		public MainStory(Activity context) {
			this.context = context;
		}

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			mProgressDialog = new ProgressDialog(context);
			mProgressDialog.setMessage("Loading full story...");
			mProgressDialog.setIndeterminate(false);
			mProgressDialog.show();
		}

		@Override
		protected Void doInBackground(Void... params) {
			try {
				Document document = Jsoup.connect(url).timeout(0).get();
				Elements newsBody = new Elements();
				newsBody.addAll((Elements) document.select("style"));
				Element emptyElement = new Element(Tag.valueOf("p"), "");
				emptyElement
						.html("The content cannot be retireved from the source. Please write to the developer");

				Elements elm = document.select(storyDivId);

				if (elm == null || elm.isEmpty()) {
					elm = document.select("#txt_628");
					if (elm == null || elm.isEmpty()) {
						elm = document.select("#txt_630");
						if (elm == null || elm.isEmpty()) {
							elm = document.select("#txt_632");
							if (elm == null || elm.isEmpty()) {
								elm = document.select("#txt_633");
								if (elm == null || elm.isEmpty()) {
									elm = document.select("#txt_634");
									if (elm == null || elm.isEmpty()) {
										elm = new Elements(emptyElement);
									}
								}
							}
						}
					}
				}
				newsBody.addAll((Elements) elm.removeAttr("style")
						.removeAttr("width").removeAttr("height"));

				Story = newsBody.toString();
			} catch (IOException e) {
				Log.e("MainStory", "error in background");
			}
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {

			Log.i("MainStory", "start in post execute");
			WebView content = (WebView) rootView
					.findViewById(R.id.fullStoryWebView);
			content.loadDataWithBaseURL(url, Story, "text/html",
					Encoding.UTF_8.toString(), null);
			WebSettings setings = content.getSettings();
			setings.setTextZoom(300);
			setings.setLoadWithOverviewMode(true);
			setings.setUseWideViewPort(true);
			setings.setBuiltInZoomControls(true);
			mProgressDialog.dismiss();
		}
	}

}
